package com.winkyle.woodenfish.presenter;

import android.util.Log;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.winkyle.woodenfish.controller.activity.PersonalDataActivity;
import com.winkyle.woodenfish.entity.HttpResult;
import com.winkyle.woodenfish.entity.UserInfo;
import com.winkyle.woodenfish.mvpview.AccountDataView;
import com.winkyle.woodenfish.mvpview.PersonalDataView;
import com.winkyle.woodenfish.retrofit.UserInfoRetrofit;
import com.winkyle.woodenfish.rx.HttpSubscriber;

import java.io.File;


/**
 * @author winkyle
 * @date 2018/06/15
 * @description  碎片类 {@link PersonalDataActivity}的p层
 */

public class PersonalDataPresenter extends BaseMvpPresenter<PersonalDataView> {
    /**
     * @description 向服务器请求个人资料信息，若请求成功
     *               则调用v层刷新个人资料方法，若失败则弹出提示框，告知原因
     * @return
     */
    public void viewPersonalData(){
        subscribeToRequest(createRetrofitApi(UserInfoRetrofit.class).findUserInfoByUserId(),
                new HttpSubscriber<PersonalDataPresenter,HttpResult<UserInfo>>(this) {
                    @Override
                    public void processData(HttpResult<UserInfo> httpResult) {
                        if(httpResult.getCode() == 1){
                            getMvpView().refreshPersonalData(httpResult.getBody());
                            return;
                        }
                        getMvpView().showRemindDialog(httpResult.getMsg());
                    }
                });

    }

    /**
     * @description 向服务器请求修改头像,若头像修改成功则调用v层修改头像成功方法
     * @param file 要发送的图片文件
     */
    public void changeHeadPortrait(File file){
        RequestBody imageFile =
                RequestBody.create(MediaType.parse("image/jpg"), file);
        subscribeToRequest(createRetrofitApi(UserInfoRetrofit.class).modifyHeadPortraitByUserId(
                imageFile),
                new HttpSubscriber<PersonalDataPresenter, HttpResult<String>>(this,true) {
                    @Override
                    public void processData(HttpResult<String> httpResult) {
                        if(httpResult.getCode() == 1){
                            getMvpView().changeHeadPortraitSucceed(httpResult.getBody());
                            return;
                        }
                        getMvpView().showRemindDialog(httpResult.getMsg());
                    }

                });
    }

}
